<extend name="Layout/page_popup" />
<block name="content">

	<div class="panel panel-default ">
		<div class="panel-heading clearfix">
			<div class="pull-left">
				<label>
					<input class="ace"  type="radio" id="rb_dept" name="type" value="dept" >
					<span class="lbl">部门</span> </label>

				<label class="display-none">
					<input  class="ace"  type="radio" id="rb_rank" name="type" value="rank">
					<span class="lbl">职级</span> </label>

				<label>
					<input  class="ace"  type="radio" id="rb_position" name="type" value="position">
					<span class="lbl">职位</span> </label>

			</div>
			<div class="pull-right">
				<a  onclick="save();" class="btn btn-sm btn-primary">确定</a>
				<a  onclick="myclose();" class="btn btn-sm btn-primary">关闭</a>
			</div>
		</div>
		<div class="panel-body">
			<div class="col-28 pull-left">
				<div class="">
					<b>地址簿</b>
				</div>
				<div class="popup_tree_menu" >
					<div id="dept" class="display-none" style="height:200px;">
						{$list_dept}
					</div>
					<div id="rank" class="display-none" style="height:200px;">
						{$list_rank}
					</div>
					<div id="position" class="display-none" style="height:200px;">
						{$list_position}
					</div>
					<div id="personal" class="display-none" style="height:200px;">
						<ul class="tree_menu">
							<volist name="list_personal" id="vo">
								<li>
									<a class="" node="{$key}"><i class="icon level1"></i><span>{$vo}</span></a>
								</li>
							</volist>
						</ul>
					</div>
				</div>
				<div>
					<div id="addr_list" style="width:100%;height:210px;"></div>
				</div>
			</div>
			<div class="col-34 pull-right">
				<div>
					<b style="padding-left: 60px;">选择收信人</b><span id="rc_count"></span>
				</div>
				<div class="clearfix" style="margin-bottom: 15px;">
					<label class="col-4 pull-left text-right" ><a onclick="add_address('rc');" class="btn btn-sm btn-primary"> <i class="fa fa-angle-double-right"></i> </a> </label>
					<div class="col-28 pull-right">
						<div id="rc" style="width:100%;height:424px;overflow:hidden">
							<select size="6" style="height:100%;width:100%;"></select>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

</block>
<block name="js">
	<script type="text/javascript">
		function save() {
			$("#rc select option").each(function(i) {
				email = ($(this).html().split(";")[1].split("&")[0]);
				emp_no = $(this).val();
				name = jQuery.trim($(this).text());
				name = name.replace(/<.*>/, '');
				html_string = conv_inputbox_item(name, emp_no);
				$("#recever .address_list", parent.document).append(html);
			});
			myclose();
		}

		function showdata(result) {
			$("#addr_list").html("");
			if ( type = $("input[name='type']:checked").val() == "dept") {
				var id = "dept_" + $("#dept a.active").attr("node");
				var dept_name = $("#dept a.active span").text();
				var email = "dept@group";
				var name = dept_name + "&lt;" + email + "&gt;";
				var html_string = conv_address_item(name,id);
				$("#addr_list").html(html_string);
			}
			for (s in result.data) {
				var id = result.data[s].id;
				var position_name = result.data[s].position_name;
				var emp_no = result.data[s].emp_no;
				var email = result.data[s].email;
				var name = result.data[s].name;
				var name = name + "/" + position_name + "&lt;" + email + "&gt;";
				var html_string = conv_address_item(name,emp_no);
				$("#addr_list").append(html_string);
			}
		}


		$(document).ready(function() {

			$("#rb_{$type}").prop('checked', true);
			// 选择用户默认选择的类型

			$("#{$type}").removeClass("display-none");

			$("input[name='type']").on('click', function() {
				$("input[name='type']").each(function() {
					$("#" + $(this).val()).addClass("display-none");
				});
				$("#" + $(this).val()).removeClass("display-none");
			});

			$(".tree_menu  a").click(function() {
				$(".tree_menu a").attr("class", "");
				var type = $("input[name='type']:checked").val();
				$(this).attr("class", "active");
				sendAjax("{:U('read')}", "type=" + type + "&id=" + $(this).attr("node"), function(data) {
					showdata(data);
				});
				return false;
				//禁止连接生效
			});

			$(document).on("dblclick", "#addr_list label", function() {
				$text = $(this).text();
				$val = $(this).find("input").val();
				if ($("#rc select option[value='" + $val + "']").val() == undefined) {
					$("<option></option>").val($val).text($text).appendTo("#rc select");
					$("#rc_count").text("(" + $("#rc select option").length + ")");
				};
			});

			$("#rc  select").on("dblclick", function() {
				$(this).find("option:selected").remove();
				$("#rc_count").text("(" + $("#rc select option").length + ")");
			});

			$("#addr_list").on("mouseover", function() {
				$("#addr_list label").draggable({
					appendTo : "body",
					helper : "clone"
				});
			});

			$("#rc select").droppable({
				activeClass : "ui-state-default",
				hoverClass : "ui-state-hover",
				accept : ":not(.ui-sortable-helper)",
				drop : function(event, ui) {
					$text = ui.draggable.text();
					$val = ui.draggable.find("input").val();
					if ($("#rc select option[value='" + $val + "']").val() == undefined) {
						$option = $("<option></option>");
						$option.val($val);
						$option.text($text).appendTo(this);
						$("#rc_count").text("(" + $("#rc select option").length + ")");
					};
				},
			}).sortable({
				items : "option:not(.placeholder)",
				sort : function() {
					$(this).removeClass("ui-state-default");
				}
			});
		});
		//最终确认

		function add_address(name) {
			$("input:checked[name='addr_id']").each(function() {
				$(this).prop('checked',false);
				$text = $(this).parents("label").find("span").text();
				$val = $(this).val();
				if ($("#" + name + " select option[value='" + $val + "']").val() == undefined) {
					$("<option></option>").val($val).text($text).appendTo("#" + name + " select");
					$("#" + name + "_count").text("(" + $("#" + name + " select option").length + ")");
				};
			});
		}

	</script>
</block>